package com.douwong.student.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.douwong.student.application.BaseApplication;
import com.douwong.student.model.AnswerRecord;

import java.util.ArrayList;


/**
 * 手写互动记录
 * @author leihuajie
 *
 */
public class HandExchangeDBService extends BDBaseService<AnswerRecord> {

//	private static HandExchangeDBService handExchangeDBService = null;

	public static HandExchangeDBService getInstance(Context context) {
//		if (handExchangeDBService == null) {
//			handExchangeDBService = new HandExchangeDBService(context);
//		}
		return new HandExchangeDBService(context);
	}

	protected HandExchangeDBService(Context context) {
		super(context);
	}

	@Override
	public void add(AnswerRecord t) {
	}

	public int addData(AnswerRecord t) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.execSQL(
				"insert into hand_exchange(name,subject,createTime,starCount,filePath,userId) values(?,?,?,?,?,?)",
				new Object[] { t.getTitleName(), t.getSubject(),
						t.getCreateTime(), t.getAwardCount(), t.getFilePath(),
						BaseApplication.sUserBean.getUserbean().getEuNumber() });
		
		Cursor cursor = db.rawQuery(
				"select * from hand_exchange where name=?",
				new String[] { t.getTitleName() });
		int id = 0;
		if (cursor != null) {
			if (cursor.moveToFirst()) {
				id = cursor.getInt(0);
			}
		}
		cursor.close();
		db.close();
		return id;
	}

	public void update(int id, AnswerRecord t) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.execSQL(
				"update hand_exchange set name=?,subject=?,createTime=?,starCount=?,filePath=? where id=?",
				new Object[] { t.getTitleName(), t.getSubject(),
						t.getCreateTime(), t.getAwardCount(), t.getFilePath(),
						id });
		db.close();
	}

	public ArrayList<AnswerRecord> query(ArrayList<AnswerRecord> list) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery(
				"select * from hand_exchange where userId=? order by id desc",
				new String[] { BaseApplication.sUserBean.getUserbean().getEuNumber() });
		while (cursor.moveToNext()) {
			AnswerRecord answerRecord = new AnswerRecord();
			answerRecord.setTitleName(cursor.getString(1));
			answerRecord.setSubject(cursor.getString(2));
			answerRecord.setCreateTime(cursor.getString(3));
			answerRecord.setAwardCount(cursor.getInt(4));
			answerRecord.setFilePath(cursor.getString(5));
			list.add(answerRecord);
		}
		return list;
	}

	@Override
	public void delete(int id) {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(int id) {

	}
	
    public void delete(String name){
	SQLiteDatabase db = dbHelper.getWritableDatabase();
	db.execSQL("delete from hand_exchange where userId=? and name=?", new String[]{BaseApplication.sUserBean.getUserbean().getEuNumber(),name});
	db.close();
    }
    
	@Override
	public AnswerRecord query(int id) {
		// TODO Auto-generated method stub
		return null;
	}

}
